<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>点餐支付</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>

<div style="width: 200px;height: 50px;margin:0 auto;margin-top: 200px;" onclick="callpay()">支付测试</div>

<script type="text/javascript">

    var payStr=null;
    $(function(){
        order();
    });

    function order(){
        $.ajax({
            type:"GET",
            url:"/wechat/payOrder?userId=o9vPqwZ6WpQudsgKAuJcJvtDYSBY&rank="+Math.random(),
            dataType:"json",
            async:false,
            timeout: 10000,
            success: function(data){
                alert(data);
                payStr=data;
                //payStr=JSON.stringify(data);
                alert(JSON.stringify(data));
            },
            error:function(e){

            }
        });
    }

    //调用微信JS api 支付
    function jsApiCall()
    {
        //var payStr = ${payStr};
        //alert(JSON.stringify(payStr));
        //payStr=JSON.stringify(payStr);
        //alert(payStr);
        WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                payStr,
                function(res){
                    alert(JSON.stringify(res));
                    //WeixinJSBridge.log(res.err_msg);
                    //alert(res.err_code+res.err_desc+res.err_msg);
                }
        );
    }

    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall();
        }
    }
</script>
</body>
</html>